#include "bigIntegerPtr.h"
#include <algorithm>

BigIntegerPtr::BigIntegerPtr(BigInteger * pointer)
{
	ptrCounter = new BigIntegerPtrCounter(pointer);
}

BigIntegerPtr::BigIntegerPtr(BigIntegerPtr const& pointer)
{
	ptrCounter = pointer.ptrCounter;
	++ptrCounter;
}

BigIntegerPtr:: ~BigIntegerPtr()
{	
	--ptrCounter;
	if(ptrCounter -> isNull())
		delete ptrCounter;
}

void BigIntegerPtr::swap(BigIntegerPtr & b)
{	
	std::swap(ptrCounter,b.ptrCounter);		
}


BigInteger & BigIntegerPtr::operator *() const
{
	return *(ptrCounter->ptr);
}

BigInteger * BigIntegerPtr::operator ->() const
{
	return ptrCounter->ptr;
}

BigIntegerPtr & BigIntegerPtr::operator = (BigInteger * pointer)
{
	--ptrCounter;

	if(ptrCounter -> isNull())
		delete ptrCounter;

	ptrCounter = new BigIntegerPtrCounter(pointer);
	return *this;
}

BigIntegerPtr & BigIntegerPtr::operator = (BigIntegerPtr const& b)
{
	if(this != &b)
	{
		BigIntegerPtr(b).swap(*this);
		return *this;		
	}
}

bool operator ==(BigIntegerPtr const& a, BigIntegerPtr const& b)
{
	return (a.ptrCounter == b.ptrCounter);
}

bool operator !=(BigIntegerPtr const& a, BigIntegerPtr const& b)
{
	return !(a.ptrCounter == b.ptrCounter);
}

bool operator >(BigIntegerPtr const& a, BigIntegerPtr const& b)
{
	return (a.ptrCounter > b.ptrCounter);
}

bool operator <(BigIntegerPtr const& a, BigIntegerPtr const& b)
{
	return (a != b && !(a > b));
}	

bool operator ==(BigIntegerPtr const& a, BigInteger const* b)
{
	return (a.ptrCounter->ptr == b);
}

bool operator !=(BigIntegerPtr const& a, BigInteger const* b)
{
	return !(a.ptrCounter->ptr == b);
}

bool operator >(BigIntegerPtr const& a, BigInteger const* b)
{
	return (a.ptrCounter->ptr > b);
}

bool operator <(BigIntegerPtr const& a, BigInteger const* b)
{
	return (a != b && !(a > b));
}

